package com.hs.core;/*
 *@author:guozhijiang
 *@create:2021-03-22 17:09
 */

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.mrl.RtspMrl;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class Forward {
    private static final Logger logger = LoggerFactory.getLogger(Forward.class);

    private  static final String NATIVE_LIBRARY_SEARCH_PATH = "classpath";
    private  static final String SERVERADDRESS = "127.0.0.1";//输出流的IP地址
    private  static final int  PORT=8899;//端口
    private  static final String ID="vedio";//视频id

    public static void start() {
        // 加载VLC
        String inVedioSrream="";
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH);
        System.out.println("==========="+LibVlc.INSTANCE.libvlc_get_version());
        //NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC\\sdk\\lib");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

        // RTSP地址
        //String mrl = new RtspMrl().host("192.168.1.xx").port(8080).path("/s?dev=xxxxx").value();
        String mrl1 = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
        inVedioSrream=mrl1;
        // 输出的HTTP地址需要的参数
        String options = formatHttpStream(SERVERADDRESS, PORT, ID);
        //System.out.println("地址"+options);

        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
        HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();

        // 进行转换
        //mediaPlayer.playMedia(mrl1, options);
        // mediaPlayer.playMedia(mrl1, options, ":sout-ts-dts-delay=100", ":sout-all", ":sout-keep");
        // System.out.println("转换后的地址："+options);

        if (inVedioSrream.startsWith("http")) {
            mediaPlayer.playMedia(inVedioSrream, options, ":sout-ts-dts-delay=100", ":sout-all", ":sout-keep");
        } else if (inVedioSrream.startsWith("rtsp") | inVedioSrream.startsWith("rtp")) {
            mediaPlayer.playMedia(inVedioSrream, options, ":no-sout-rtp-sap",
                    ":sout-ts-dts-delay=100",
                    ":no-sout-standard-sap",
                    ":sout-all",
                    ":sout-keep");
        }
        System.out.println("视频流的输出地址：http://"+SERVERADDRESS+":"+PORT+"/"+ID);
        // 挂起
        try {
            Thread.currentThread().join();//阻塞当前线程
            logger.debug("视频流转发线程已挂起");

        } catch (InterruptedException e) {
            e.printStackTrace();
            logger.debug("转发线程异常");
        }

    }
    /**
     *      * @Description: 转换HTTP输出地址 
     *      * @param serverAddress    IP地址
     *      * @param serverPort    端口号
     *      * @param id    标识，以免多个流混 
     *     
     */
    private static String formatHttpStream(String serverAddress, int serverPort, String id) {
        StringBuilder sb = new StringBuilder(60);
        //参数。需要根据要求再做修改（此参数会影响转化后视频的质量和传播速度）
        sb.append(":sout=#transcode{vcodec=theo,vb=2200,scale=1,acodec=vorb,ab=32,channels=2,samplerate=44100} :duplicate{dst=std{access=http,mux=ogg,");
        sb.append("dst=");
        sb.append(serverAddress);
        sb.append(':');
        sb.append(serverPort);
        sb.append("/");
        sb.append(id);
        sb.append("}}");
        return sb.toString();
    }

    public static void main(String[] args) {
        start();
    }

}
